package com.zzc.fwzpxt.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.Banner;
import com.zzc.fwzpxt.service.IBannerService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import com.zzc.common.utils.StringUtil;
import com.zzc.sys.entity.Role;
import com.zzc.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 轮播 banner 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-10-09
 */
@Controller
@RequestMapping("/admin/banner")
public class BannerController extends BaseController {

    @Autowired
    private IBannerService iBannerService;

    /**
     * 轮播列表页面
     * @return
     */
    @GetMapping("/list")
    public String list(Model model) {

        return blogPath+"banner/list";
    }

    /**
     * 获取轮播列表
     * @param pageNumber 当前页
     * @param pageSize 每页显示条数
     * @param searchText 搜索名称
     * @return
     */
    @ResponseBody
    @PostMapping("/getList")
    public Map<String, Object> getUserList(int pageNumber, int pageSize, String searchText) {
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Banner> page = new Page<>(pageNumber, pageSize);
        EntityWrapper<Banner> wrapper = new EntityWrapper<>();
        wrapper.orderBy("sort", true);
        Page<Banner> bannerPage = iBannerService.selectPage(page, wrapper);
        result.put("total", bannerPage.getTotal());
        result.put("rows", bannerPage.getRecords());
        return result;
    }

    /**
     * 添加轮播
     * @param banner
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addBanner")
    public JsonResult addBanner(Banner banner) {
        return  iBannerService.insert(banner) ? renderSuccess("添加成功") : renderError("添加失败");
    }

    @RequestMapping(value = "/updateBanner/{id}")
    public String updateBannerView(@PathVariable(value = "id") Long id, Model model) {
        Banner banner = iBannerService.selectById(id);
        model.addAttribute("banner", banner);
        return blogPath+"banner/updateBanner";
    }

    /**
     * 修改轮播
     * @param banner
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateBanner")
    public JsonResult updateBanner(Banner banner) {
        return iBannerService.updateById(banner) ? renderSuccess("修改成功") : renderError("修改失败");
    }

    /**
     * 删除轮播
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) Long id) {
        if (null == id) {
            return renderError("请选择数据");
        }
        return iBannerService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }

}
